//
//  UserViewController.m
//  News
//
//  Created by 宋浩文的pro on 16/5/22.
//  Copyright © 2016年 songhaowen. All rights reserved.
//

#import "UserViewController.h"
#import "LoginViewController.h"
#import "CommonTool.h"
#import "RegisterViewController.h"
#import "MBProgressHUD.h"
#import "MainTabBarController.h"

@interface UserViewController ()


@property (weak, nonatomic) IBOutlet UITextField *accountTF;

@property (weak, nonatomic) IBOutlet UITextField *passwordTF;

@property (weak, nonatomic) IBOutlet UIButton *loginBtn;

@property (weak, nonatomic) IBOutlet UIButton *phoneLoginBtn;

@property (weak, nonatomic) IBOutlet UIButton *registerBtn;

@end

@implementation UserViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self initUI];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}

- (void)initUI
{
    _loginBtn.layer.cornerRadius = 5;
    _registerBtn.layer.cornerRadius = 5;
}



- (IBAction)login:(UIButton *)btn
{
    [self.view endEditing:YES];
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:Window animated:YES];
    hud.labelText = @"登录中...";
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [BmobUser loginWithUsernameInBackground:_accountTF.text password:_passwordTF.text block:^(BmobUser *user, NSError *error) {
            if (!error) {
                [hud hide:YES];
                [UIApplication sharedApplication].keyWindow.rootViewController = [[MainTabBarController alloc] init];
            } else {
                [hud hide:YES];
                [CommonTool showError:@"账号密码错误"];
            }
        }];
    });
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

- (IBAction)registerBtnClicked:(id)sender {
    RegisterViewController *registerVC = [[RegisterViewController alloc] init];
    [self.navigationController pushViewController:registerVC animated:YES];
}

- (IBAction)phoneLogin:(UIButton *)btn
{
    LoginViewController *phoneLoginVC = [[LoginViewController alloc] init];
    
    [self.navigationController pushViewController:phoneLoginVC animated:YES];
    
}



@end
